package cn.hss.thread;

/**
 * @author 作者：HeShaoShuai
 * @Email 邮箱：2569819541@qq.com
 * @Date 日期：2023/4/28 15:38
 * @Package 包名：cn.hss.thread
 * @Project 项目名：JavaSE
 * @Version 1.0
 */
public class DeadLockTest {
	public static void main(String[] args) {
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		//创建线程
		new Thread(){
			@Override
			public void run() {
				synchronized (sb1){
					sb1.append("a");
					sb2.append("1");
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (sb2){
					sb1.append("b");
					sb2.append("2");
					System.out.println(sb1);
					System.out.println(sb2);
				}
			}
		}.start();
		//创建线程
		new Thread(){
			@Override
			public void run() {
				synchronized (sb2){
					sb1.append("c");
					sb2.append("3");
				}
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (sb1){
					sb1.append("d");
					sb2.append("4");
					System.out.println(sb1);
					System.out.println(sb2);
				}
			}
		}.start();
	}
}